﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EveryJuHuInfoItem : MonoBehaviour {
    EginMahjong mahjong = EginMahjong.Instance;
    EginUser user = EginUser.Instance;
   
    public UISprite avatarSprit;
    public UILabel username;

    //scroe
    public GameObject Score;
    public GameObject ScoreFather;
    public UISprite jiajian;


    public UISprite huorzimo;    
    public Transform cardFather;
    private float xControl = -206;
    private string CardInfo;
    public bool isflow;
    private int seatID1;
    private void Start()
    {
       
    }
    public void LoadInfo(int seatID,List<GameOverUserInfo> gameOverUserInfo) {
        seatID1 = seatID;
        //用户名字
        for (int i = 0; i < gameOverUserInfo.Count; i++) {
            if (seatID == gameOverUserInfo[i].SeatId) {
                for(int j = 0;j< user.gameUserInfoList.Count;j++){
                    if(seatID == user.gameUserInfoList[j].SeatId){
                        avatarSprit.spriteName = user.gameUserInfoList[j].AvatarId.ToString();
                        username.text = user.gameUserInfoList[j].Nickname;
                    }
                }
                
            }
        }
        
        
        //score.text =user.ReturnRelCoin( mahjong.OverRoundScore[seatID]);
        int tempScore = mahjong.OverRoundScore[seatID];
        string tempStr = user.ReturnRelCoin(Mathf.Abs(mahjong.OverRoundScore[seatID]));
        Debug.Log(tempStr);
        float x = Score.transform.localPosition.x;
        float y = Score.transform.localPosition.y;
        if (tempScore >= 0)
        {
            jiajian.gameObject.SetActive(true);
            jiajian.spriteName = "plus_" + "jia";
            int len = tempStr.Length;
            for (int j = 0; j < len; j++)
            {
                GameObject obj = (GameObject)Instantiate(Score);
                obj.SetActive(true);
                obj.transform.parent = ScoreFather.transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
                string str = tempStr.Substring(0, 1);
                if (j == 0)
                {
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                else
                {
                    if (str == ".")
                    {
                        x += 22;

                    }
                    else if (str == "w")
                    {
                        x += 40;
                    }
                    else
                    {
                        x += 31;
                    }
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                tempStr = tempStr.Remove(0, 1);
                obj.GetComponent<UISprite>().spriteName = "plus_" + str;
            }
        }
        else
        {
            jiajian.gameObject.SetActive(true);
            jiajian.spriteName = "less_" + "jian";
            int len = tempStr.Length;
            for (int j = 0; j < len; j++)
            {
                GameObject obj = (GameObject)Instantiate(Score);
                obj.SetActive(true);
                obj.transform.parent = ScoreFather.transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
                string str = tempStr.Substring(0, 1);
                if (j == 0)
                {
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                else
                {
                    if (str == ".")
                    {
                        x += 22;
                    }
                    else if (str == "w")
                    {
                        x += 40;
                    }
                    else
                    {
                        x += 31;
                    }
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                tempStr = tempStr.Remove(0, 1);
                obj.GetComponent<UISprite>().spriteName = "less_" + str;
            }
        }

        string tempStr1 = "";
        if (mahjong.OverRobot[seatID])
        {
            tempStr1 = "已托管   ";
        }

      
        
        int tempPeng=0;
        int tempLightGang=0;
        int tempDarkGang=0;

        #region 显示碰 杠 暗杠碰x+115 杠x+150 user.OverMahjongSpecialCard
        if (mahjong.OverMahjongSpecialCard[seatID].Count > 0) {
            for (int i = 0; i < mahjong.OverMahjongSpecialCard[seatID].Count; i++)
            {
                if (mahjong.OverMahjongSpecialCard[seatID][i].ActionType == MAHJONG_ACTION_TYPE.Peng)
                {
                    GameObject pengCard = LoadModelReturnObj("GameMahjong/NextGameP/SpecialCard/EveryJuHu/pengCards", cardFather);
                    ItemPengCards itemPengCards = pengCard.GetComponent<ItemPengCards>();
                    xControl += 115;
                    pengCard.transform.localPosition = new Vector3(xControl, 0, 0);

                    for (int j = 0; j < 3; j++)
                    {
                        itemPengCards.pengCards[j].spriteName = mahjong.OverMahjongSpecialCard[seatID][i].TargetCard.ToString();
                    }
                    tempPeng++;
                }
                else if (mahjong.OverMahjongSpecialCard[seatID][i].ActionType == MAHJONG_ACTION_TYPE.AddGang || mahjong.OverMahjongSpecialCard[seatID][i].ActionType == MAHJONG_ACTION_TYPE.LightGang)
                {
                    GameObject gangcards = LoadModelReturnObj("GameMahjong/NextGameP/SpecialCard/EveryJuHu/gangcards", cardFather);
                    ItemGangCards itemGangCards = gangcards.GetComponent<ItemGangCards>();
                    xControl += 150;
                    gangcards.transform.localPosition = new Vector3(xControl, 0, 0);
                    for (int j = 0; j < 4; j++)
                    {
                        itemGangCards.gangCards[j].spriteName = mahjong.OverMahjongSpecialCard[seatID][i].TargetCard.ToString();
                    }
                    tempLightGang++;
                }
                else if (mahjong.OverMahjongSpecialCard[seatID][i].ActionType == MAHJONG_ACTION_TYPE.DarkGang)
                {
                    GameObject darkgangcards = LoadModelReturnObj("GameMahjong/NextGameP/SpecialCard/EveryJuHu/darkgangcards", cardFather);
                    ItemDrakGangCards itemDrakGangCards = darkgangcards.GetComponent<ItemDrakGangCards>();
                    xControl += 115;
                    darkgangcards.transform.localPosition = new Vector3(xControl, 0, 0);
                    itemDrakGangCards.angangCard.spriteName = mahjong.OverMahjongSpecialCard[seatID][i].TargetCard.ToString();
                    tempDarkGang++;
                }

                
            }
        }

        showHandCard();
        #endregion
        Debug.Log("结算信息显示完毕");


        //显示明杠 暗杠label
        if (CardInfo == "")
        {
            if (tempPeng > 0)
            {
                
            }
            if (tempLightGang > 0)
            {
                CardInfo = CardInfo + "明杠x" + tempLightGang;
            }
            if (tempDarkGang > 0)
            {
                CardInfo = CardInfo + "暗杠x" + tempDarkGang;
            }
        }
        else {
            if (tempPeng > 0)
            {
                
            }
            if (tempLightGang > 0)
            {
                CardInfo = CardInfo + "   明杠x" + tempLightGang;
            }
            if (tempDarkGang > 0)
            {
                CardInfo = CardInfo + "   暗杠x" + tempDarkGang;
            }
        }

        //LackMultiple.text = tempStr1+ CardInfo;



        //显示局数

       
    }

    void showHandCard() {

        #region //显示手中牌 //user.OverHandCardList


        BubbleSort(mahjong.OverHandCardList[seatID1]);
        GameObject outcard = LoadModelReturnObj("GameMahjong/NextGameP/SpecialCard/EveryJuHu/outcards", cardFather);
        xControl += mahjong.OverHandCardList[seatID1].Count * 33 + 16;
        outcard.transform.localPosition = new Vector3(xControl, 0, 0);
        ItemOutCards itemOutCards = outcard.GetComponent<ItemOutCards>();
        for (int i = 0; i < mahjong.OverHandCardList[seatID1].Count; i++)
        {
            itemOutCards.outCards[i].gameObject.SetActive(true);
            itemOutCards.outCards[i].spriteName = mahjong.OverHandCardList[seatID1][i].ToString();
        }
        //Debug.Log("user.Huseatid"+user.Huseatid);
        //显示胡牌或者自摸 hu的牌
        if (mahjong.Huseatid == seatID1 && !isflow)
        {
            huorzimo.gameObject.SetActive(true);
            if (mahjong.whatHu == MAHJONG_ACTION_TYPE.SelfHu || mahjong.whatHu == MAHJONG_ACTION_TYPE.GangHu)
            {
                //自摸
                huorzimo.spriteName = "zimo";
            }
            else if (mahjong.whatHu == MAHJONG_ACTION_TYPE.NormalHu)
            {
                //胡
                huorzimo.spriteName = "hu";
            }

            GameObject hu = LoadModelReturnObj("GameMahjong/NextGameP/SpecialCard/EveryJuHu/hu", cardFather);
            xControl += 33 + 16;
            hu.transform.localPosition = new Vector3(xControl, 0, 0);
            ItemPengCards hucards = hu.GetComponent<ItemPengCards>();
            hucards.pengCards[0].spriteName = mahjong.HuCard.ToString();
        }
        
        #endregion
    }
   
    GameObject LoadModelReturnObj(string TempPath, Transform fatherTrans)  // fatherTrans 深度统一
    {
        Object obj = Resources.Load(TempPath, typeof(GameObject));
        GameObject go = Instantiate(obj, transform.position, transform.rotation, fatherTrans) as GameObject;
        return go;
    }

    #region 冒泡排序大到小
    private void BubbleSort(List<int> R)
    {
        int len = R.Count;
        bool flag = false;
        for (int i = 0; i < len - 1; i++)
        {
            flag = false;
            for (int j = 0; j < len - i - 1; j++)
            {
                if (R[j] < R[j + 1])
                {
                    Swap(R[j], R[j + 1], j, R);
                    flag = true;
                }
            }
            if (!flag)
            {
                break;
            }
        }

    }
    private void Swap(int left, int right, int j, List<int> R)
    {
        int temp = left;
        R[j] = right;
        R[j + 1] = temp;
    }
    #endregion
}
